package com.jay.beiange.efccp;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jay.beiange.efccp.bean.ProductDetail;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/12/25 0025.
 */

public class ProductDetailActivity extends Activity {
    private Toolbar mToolbar;
    private TextView mToolbarTitle;
    private WebView mContent;
    ProgressDialog pdialog;
    private ImageLoader imageLoader = ImageLoader.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);
        initView();
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.id_toolbar);
        mToolbarTitle = (TextView) mToolbar.findViewById(R.id.id_toolbar_title);
        mToolbar.setNavigationIcon(R.mipmap.left_arrow);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mContent = (WebView) findViewById(R.id.id_tv_product_detail_content);



        Intent intent1 = getIntent();
        Bundle bundle = new Bundle();
        bundle = this.getIntent().getExtras();
        int id = bundle.getInt("id");


        Toast.makeText(getApplicationContext(),""+id+"",Toast.LENGTH_LONG).show();

        pdialog = new ProgressDialog(ProductDetailActivity.this);
        pdialog.setMessage("正在加载");
        pdialog.show();


        OkHttpClient mOkHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url("http://192.168.22.15/app_dev.php/open/product/"+id+"")
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "网络连接失败", Toast.LENGTH_SHORT).show();

                    }
                });
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String res = response.body().string();
                pdialog.dismiss();
                Gson gson = new Gson();
                ProductDetail productDetail = gson.fromJson(res, ProductDetail.class);
                final String title = productDetail.getData().getTitle();
                final String content = productDetail.getData().getContent();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mToolbar.setTitle("" + title + "");
                        String mimeType = "text/html";
                        String encoding = "utf-8";
                        mContent.loadDataWithBaseURL("null", content, mimeType, encoding, "");
                        WebSettings webSettings= mContent.getSettings(); // webView: 类WebView的实例
                        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                        webSettings.setUseWideViewPort(true);
                        webSettings.setLoadWithOverviewMode(true);



                    }

                });

            }
        });


    }


}
